package com.baoer.controller;

import com.baoer.R;
import com.baoer.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Chen Feiya
 * @CreateTime: 2025-07-01
 * @Description:
 * @Version: 1.0
 */

@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public R login(@RequestBody Map<String, Object> params) {
        System.out.println("params:"+params);
        Integer userid = (Integer) params.get("userid");
        String password = (String) params.get("password");
        Map Rtoken = loginService.login(userid, password);
        System.out.println("token:"+ Rtoken.get("token"));
        String token = (String) Rtoken.get("token");
        if(token != null) {
            System.out.println("Rtoken:"+Rtoken);
            return R.ok(Rtoken);
        }else{
            return (R) Rtoken;
        }
    }

//    @PostMapping("/register")
//    public R register(@RequestBody User user) {
//        boolean success = loginService.register(user);
//
//        Map<String, Object> result = new HashMap<>();
//        result.put("code", success ? 200 : 400);
//        result.put("message", success ? "注册成功" : "注册失败");
//
//        return R.ok(result);
//    }
    
    @RequestMapping(value="/hello")
    public String hello(){
        return "success";
    }
}